<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
  <head>
    <title>KRPC::Encoder</title>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
    <link href='../../css/style.css' media='screen' rel='stylesheet' type='text/css'>
    <script type='text/javascript'>
      //<![CDATA[
        function popupCode(url) {
          window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
        }
        
        function toggleCode(id) {
          var code = document.getElementById(id)
        
          code.style.display = code.style.display != 'block' ? 'block' : 'none'
          return true
        }
        
        // Make codeblocks hidden by default
        document.writeln('<' + 'style type="text/css">.method .source pre { display: none }<\/style>')
      //]]>
    </script>
  </head>
  <body class='page'>
    <div class='class' id='wrapper'>
      <div class='header'>
        <h1 class='name'>
          <span class='type'>module</span>
          KRPC::Encoder
        </h1>
        <ol class='paths'>
          <li>
            <a target="docwin" href="../../files/lib/krpc/encoder_rb.html">lib/krpc/encoder.rb</a>
          </li>
        </ol>
      </div>
      <div id='content'>
        <div id='text'>
          <div id='description'></div>
          <div id='method-list'>
            <h2>Methods</h2>
            <h3>Public Class</h3>
            <ol>
              <li><a target="docwin" href="#method-c-encode">encode</a></li>
              <li><a target="docwin" href="#method-c-encode_request">encode_request</a></li>
              <li><a target="docwin" href="#method-c-encode_value">encode_value</a></li>
            </ol>
          </div>
          <div id='context'>
          </div>
          <div id='section'>
            <div id='constants-list'>
              <h2>Constants</h2>
              <div class='name-list'>
                <table summary='Constants'>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>NAME_LENGTH</td>
                    <td>=</td>
                    <td class='context-item-value'>32</td>
                    <td>&nbsp;</td>
                    <td class='context-item-desc'></td>
                  </tr>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>RPC_HELLO_MESSAGE</td>
                    <td>=</td>
                    <td class='context-item-value'>"\x48\x45\x4C\x4C\x4F\x2D\x52\x50\x43\x00\x00\x00"</td>
                    <td>&nbsp;</td>
                    <td class='context-item-desc'></td>
                  </tr>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>STREAM_HELLO_MESSAGE</td>
                    <td>=</td>
                    <td class='context-item-value'>"\x48\x45\x4C\x4C\x4F\x2D\x53\x54\x52\x45\x41\x4D"</td>
                    <td>&nbsp;</td>
                    <td class='context-item-desc'></td>
                  </tr>
                </table>
              </div>
            </div>
            <div id='methods'>
              <h2>Public Class methods</h2>
              <div class='method public-class' id='method-method-c-encode'>
                <a name='method-c-encode'></a>
                <div class='synopsis'>
                  <span class='name'>encode</span>
                  <span class='arguments'>(obj, type)</span>
                </div>
                <div class='description'>
                  
                  <p>Given a type object, and ruby object, encode the ruby object</p>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-encode-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-encode-source'><span class="ruby-comment"># File lib/krpc/encoder.rb, line 12</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">encode</span>(<span class="ruby-identifier">obj</span>, <span class="ruby-identifier">type</span>)&#x000A;  <span class="ruby-keyword">if</span> <span class="ruby-identifier">type</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Types</span><span class="ruby-operator">::</span><span class="ruby-constant">MessageType</span>) <span class="ruby-keyword">then</span> <span class="ruby-identifier">obj</span>.<span class="ruby-identifier">serialize_to_string</span>&#x000A;  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">type</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Types</span><span class="ruby-operator">::</span><span class="ruby-constant">ValueType</span>) <span class="ruby-keyword">then</span> <span class="ruby-identifier">encode_value</span>(<span class="ruby-identifier">obj</span>, <span class="ruby-identifier">type</span>)&#x000A;  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">type</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Types</span><span class="ruby-operator">::</span><span class="ruby-constant">EnumType</span>)&#x000A;    <span class="ruby-identifier">enum_value</span> = <span class="ruby-identifier">type</span>.<span class="ruby-identifier">ruby_type</span>[<span class="ruby-identifier">obj</span>]&#x000A;    <span class="ruby-identifier">encode_value</span>(<span class="ruby-identifier">enum_value</span>, <span class="ruby-constant">TypeStore</span>[<span class="ruby-string">&quot;int32&quot;</span>])&#x000A;  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">type</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Types</span><span class="ruby-operator">::</span><span class="ruby-constant">ClassType</span>)&#x000A;    <span class="ruby-identifier">remote_oid</span> = <span class="ruby-keyword">if</span> <span class="ruby-identifier">obj</span> <span class="ruby-operator">==</span> <span class="ruby-keyword">nil</span> <span class="ruby-keyword">then</span> <span class="ruby-value">0</span> <span class="ruby-keyword">else</span> <span class="ruby-identifier">obj</span>.<span class="ruby-identifier">remote_oid</span> <span class="ruby-keyword">end</span>&#x000A;    <span class="ruby-identifier">encode_value</span>(<span class="ruby-identifier">remote_oid</span>, <span class="ruby-constant">TypeStore</span>[<span class="ruby-string">&quot;uint64&quot;</span>])&#x000A;  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">type</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Types</span><span class="ruby-operator">::</span><span class="ruby-constant">ListType</span>)&#x000A;    <span class="ruby-identifier">msg</span> = <span class="ruby-constant">TypeStore</span>[<span class="ruby-string">&quot;KRPC.List&quot;</span>].<span class="ruby-identifier">ruby_type</span>.<span class="ruby-identifier">new</span>&#x000A;    <span class="ruby-identifier">msg</span>.<span class="ruby-identifier">items</span> = <span class="ruby-identifier">obj</span>.<span class="ruby-identifier">map</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span> <span class="ruby-identifier">encode</span>( <span class="ruby-constant">TypeStore</span>.<span class="ruby-identifier">coerce_to</span>(<span class="ruby-identifier">x</span>, <span class="ruby-identifier">type</span>.<span class="ruby-identifier">value_type</span>), <span class="ruby-identifier">type</span>.<span class="ruby-identifier">value_type</span> )}.<span class="ruby-identifier">to_a</span>&#x000A;    <span class="ruby-identifier">msg</span>.<span class="ruby-identifier">serialize_to_string</span>&#x000A;  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">type</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Types</span><span class="ruby-operator">::</span><span class="ruby-constant">DictionaryType</span>)&#x000A;    <span class="ruby-identifier">entry_type</span> = <span class="ruby-constant">TypeStore</span>[<span class="ruby-string">&quot;KRPC.DictionaryEntry&quot;</span>].<span class="ruby-identifier">ruby_type</span>&#x000A;    <span class="ruby-identifier">msg</span> = <span class="ruby-constant">TypeStore</span>[<span class="ruby-string">&quot;KRPC.Dictionary&quot;</span>].<span class="ruby-identifier">ruby_type</span>.<span class="ruby-identifier">new</span>&#x000A;    <span class="ruby-identifier">msg</span>.<span class="ruby-identifier">entries</span> = <span class="ruby-identifier">obj</span>.<span class="ruby-identifier">map</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">k</span>,<span class="ruby-identifier">v</span><span class="ruby-operator">|</span>&#x000A;      <span class="ruby-identifier">entry</span> = <span class="ruby-identifier">entry_type</span>.<span class="ruby-identifier">new</span>&#x000A;      <span class="ruby-identifier">entry</span>.<span class="ruby-identifier">key</span> = <span class="ruby-identifier">encode</span>( <span class="ruby-constant">TypeStore</span>.<span class="ruby-identifier">coerce_to</span>(<span class="ruby-identifier">k</span>, <span class="ruby-identifier">type</span>.<span class="ruby-identifier">key_type</span>), <span class="ruby-identifier">type</span>.<span class="ruby-identifier">key_type</span> )&#x000A;      <span class="ruby-identifier">entry</span>.<span class="ruby-identifier">value</span> = <span class="ruby-identifier">encode</span>( <span class="ruby-constant">TypeStore</span>.<span class="ruby-identifier">coerce_to</span>(<span class="ruby-identifier">v</span>, <span class="ruby-identifier">type</span>.<span class="ruby-identifier">value_type</span>), <span class="ruby-identifier">type</span>.<span class="ruby-identifier">value_type</span> )&#x000A;      <span class="ruby-identifier">entry</span>&#x000A;    <span class="ruby-keyword">end</span>&#x000A;    <span class="ruby-identifier">msg</span>.<span class="ruby-identifier">serialize_to_string</span>&#x000A;  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">type</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Types</span><span class="ruby-operator">::</span><span class="ruby-constant">SetType</span>)&#x000A;    <span class="ruby-identifier">msg</span> = <span class="ruby-constant">TypeStore</span>[<span class="ruby-string">&quot;KRPC.Set&quot;</span>].<span class="ruby-identifier">ruby_type</span>.<span class="ruby-identifier">new</span>&#x000A;    <span class="ruby-identifier">msg</span>.<span class="ruby-identifier">items</span> = <span class="ruby-identifier">obj</span>.<span class="ruby-identifier">map</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span> <span class="ruby-identifier">encode</span>( <span class="ruby-constant">TypeStore</span>.<span class="ruby-identifier">coerce_to</span>(<span class="ruby-identifier">x</span>, <span class="ruby-identifier">type</span>.<span class="ruby-identifier">value_type</span>), <span class="ruby-identifier">type</span>.<span class="ruby-identifier">value_type</span> )}.<span class="ruby-identifier">to_a</span>&#x000A;    <span class="ruby-identifier">msg</span>.<span class="ruby-identifier">serialize_to_string</span>&#x000A;  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">type</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Types</span><span class="ruby-operator">::</span><span class="ruby-constant">TupleType</span>)&#x000A;    <span class="ruby-identifier">msg</span> = <span class="ruby-constant">TypeStore</span>[<span class="ruby-string">&quot;KRPC.Tuple&quot;</span>].<span class="ruby-identifier">ruby_type</span>.<span class="ruby-identifier">new</span>&#x000A;    <span class="ruby-identifier">msg</span>.<span class="ruby-identifier">items</span> = <span class="ruby-identifier">obj</span>.<span class="ruby-identifier">zip</span>(<span class="ruby-identifier">type</span>.<span class="ruby-identifier">value_types</span>).<span class="ruby-identifier">map</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">x</span>,<span class="ruby-identifier">t</span><span class="ruby-operator">|</span> <span class="ruby-identifier">encode</span>( <span class="ruby-constant">TypeStore</span>.<span class="ruby-identifier">coerce_to</span>(<span class="ruby-identifier">x</span>, <span class="ruby-identifier">t</span>), <span class="ruby-identifier">t</span> )}.<span class="ruby-identifier">to_a</span>&#x000A;    <span class="ruby-identifier">msg</span>.<span class="ruby-identifier">serialize_to_string</span>&#x000A;  <span class="ruby-keyword">else</span> <span class="ruby-identifier">raise</span>(<span class="ruby-constant">RuntimeError</span>, <span class="ruby-node">&quot;Cannot encode object #{obj} of type #{type}&quot;</span>)&#x000A;  <span class="ruby-keyword">end</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-class' id='method-method-c-encode_request'>
                <a name='method-c-encode_request'></a>
                <div class='synopsis'>
                  <span class='name'>encode_request</span>
                  <span class='arguments'>(req)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-encode_request-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-encode_request-source'><span class="ruby-comment"># File lib/krpc/encoder.rb, line 51</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">encode_request</span>(<span class="ruby-identifier">req</span>)&#x000A;  <span class="ruby-identifier">data</span> = <span class="ruby-identifier">req</span>.<span class="ruby-identifier">serialize_to_string</span>&#x000A;  <span class="ruby-identifier">length</span> = <span class="ruby-constant">ProtobufUtils</span><span class="ruby-operator">::</span><span class="ruby-constant">Encoder</span>.<span class="ruby-identifier">encode_nonnegative_varint</span>(<span class="ruby-identifier">data</span>.<span class="ruby-identifier">length</span>)&#x000A;  <span class="ruby-identifier">length</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">data</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-class' id='method-method-c-encode_value'>
                <a name='method-c-encode_value'></a>
                <div class='synopsis'>
                  <span class='name'>encode_value</span>
                  <span class='arguments'>(value, type)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-encode_value-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-encode_value-source'><span class="ruby-comment"># File lib/krpc/encoder.rb, line 47</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">encode_value</span>(<span class="ruby-identifier">value</span>, <span class="ruby-identifier">type</span>)&#x000A;  <span class="ruby-constant">ProtobufUtils</span><span class="ruby-operator">::</span><span class="ruby-constant">Encoder</span>.<span class="ruby-identifier">encode</span>(<span class="ruby-identifier">value</span>, <span class="ruby-identifier">type</span>.<span class="ruby-identifier">protobuf_type</span>)&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div id='footer-push'></div>
    </div>
    <div id='footer'>
      <a target="docwin" href="http://github.com/mislav/hanna/tree/master"><strong>Hanna</strong> RDoc template</a>
    </div>
  </body>
</html>
